import cProfile
import pstats
import io
import os
import sys
from main import main as wc_main

#定义要测试的大文件路径
LARGE_FILE_PATH = os.path.join("..", "examples", "performance_test.txt")

#测试处理自然语言文本的基础功能集合 --all
ARGS_TO_TEST = ['main.py', '--all', LARGE_FILE_PATH] 

def run_performance_test():
    
    original_stdout = sys.stdout
    sys.stdout = io.StringIO()
    
    original_argv = sys.argv
    sys.argv = ARGS_TO_TEST
    
    profiler = cProfile.Profile()
    profiler.enable()
    
    try:
        wc_main()
    except SystemExit:
        pass
    
    profiler.disable()
    
    sys.argv = original_argv
    sys.stdout = original_stdout

    print("\n--性能分析--:")
    
    s = io.StringIO()
    stats = pstats.Stats(profiler, stream=s).sort_stats('cumulative')
    stats.print_stats(15)
    
    print(s.getvalue())
    
    print("报告按 'cumulative time' (累计耗时) 排序，显示了最慢的15个函数。")

if __name__ == '__main__':
    if not os.path.exists('main.py'):
        print("错误：请在v0.3目录下运行此脚本。")
    else:
        run_performance_test()